<?php
/**
 * fly
 * Copyright By 5920Team (http://www.5920.me)
 * User: yu@5920.me
 * Date: 13-9-27
 * Time: 上午2:51
 */


class IndexModule extends Module
{

    /**
     * @var Db
     */
    private $db;

    /**
     * @var array
     */
    public $actionList = array();

    /**
     * @var array
     * actionName => methodName array
     */
    public $beginMethodList = array(
        'run' => array('initDb')
    );

    public function  initDb()
    {
        $this->db = new Db($GLOBALS['fly']['db']);
        //$this->db->

    }


    public function run()
    {
        $TPL = new Template($GLOBALS['fly']['template']);
        $TPL->assign('site_name', '更好用的PHP框架 - Fly');
        $TPL->assign('site_intro', '我是一个圆梦者，Fly的作者。');

        $blog = array(
            'title' => '更好用的PHP框架',
            'content' => nl2br('
我喜欢的，是自由，是简洁，是属于自己的标签。
Fly和大多数的我们一样。
不完美，却趋向完美；不成功，却趋向成功！
因为有一颗永不妥协的内心！
才能不惧改变的艰辛，Fly才能不期而至！
Fly和你我一同成长。
'),
            'info' => array(
                'addtime' => '2013.10.1',
                'author' => '小鱼',
                'weibo' => 'http://t.qq.com/yu5920me',
            ),
        );

        $links = array(
            '5920.me' => 'http://www.5920.me/',
            'OR' => 'http://t.qq.com/dial-wap',
            '熊仔' => 'http://user.qzone.qq.com/4085801'
        );


        $TPL->assign('blog', $blog);

        $TPL->assign('links', $links);
        $TPL->assign('boo1', time() < mktime(0, 0, 0, 10, 2, 2013));
        $TPL->assign('boo2', false);
        $TPL->display('index');
//        $tmp->display('admin/index');

//        echo '<br>';
//        echo $this->db->delete('haha')->where('not in (select * from ?) or id = ?',1,2);
//        echo '<br>';
//        echo $this->db->insert('sell')
//            ->rows(
//                array(
//                    array('shop_id' => '9','user_id' => '7'),
//                    array('shop_id' => '8','user_id' => '6')
//                )
//            );
//        echo '<br>';
//        echo $this->db->select('shop_id', 'user_id')->from('sell')->order('id')->page(2,5);
//        echo '<br>';
//        echo $this->db->update('haha')->row(array('title' => 'haha'))->pri(1);
//
//        echo $this->db->select()->from('sell')->where('user_id = ? ','\' delete ');
//        var_dump($this->db->getCount($this->db->select()->from('sell')->where('user_id = 7')));
        //var_dump($this->db->fetchAll($this->db->select()->from('sell')->order('id','DESC')->page(1,2)));
        //var_dump($this->db->fetchRow($this->db->select()->from('sell')->pri(1)));
        //echo ;

        //var_dump($this->db->query($this->db->insert('sell')->row(array('shop_id' => '9'))));
        /*
        var_dump($this->db->query(
            $this->db->insert('sell')
                ->rows(
                    array(
                        array('shop_id' => '9','user_id' => '7'),
                        array('shop_id' => '8','user_id' => '6')
                    )
                )
        ));
        */


        //var_dump($this->db->query($this->db->update('sell')->row(array('shop_id' => '12'))));

        //echo $this->db->delete('sell')->pri(1);
        //echo $this->db->delete('sell')->pri(array(2,3));
        //var_dump($this->db->query($this->db->delete('sell')->pri(array(2,3))));


/*
        // return 2 arrays fetch Rows  evertPage Num = 20, return on 10 pages.
        $result =  $this->db->fetchAll(
            $this->db
                ->select('title', 'text', 'type')
                ->from('tableName')
                ->where('type = ?', 'post')
                ->order('id', 'DESC')
                ->page(10, 20)
        );
        // return 2 arrays fetch Rows
        $result =  $this->db->fetchAll(
            $this->db
                ->select('title', 'text', 'type')
                ->from('tableName')
                ->where('type = ?', 'post')
                ->order('id', 'DESC')
                ->offset(20)
                ->limit(10)
        );
        // return array fetch Row
        $result =  $this->db->fetchRow($this->db
                ->select('title', 'text', 'type')
                ->from('tableName')
                ->where('id = ?', '1')
        );




        // return bool
        $this->db->query($this->db->insert('tableName')
                ->rows(
                    array(
                        array('title' => 'haha','text' => 'test',),
                        array('title' => 'haha')
                    )
                )
        );

        // return insert id
        $this->db->query($this->db->insert('tableName')
            ->row(
                    array(
                        'title' => 'haha',
                        'text' => 'test',
                    )
                )
        );
        /**
         *
         *       失败的方法，繁琐
         *
        $this->db->query($this->db->insert('tableName')
                ->field('title','content')
                ->row('haha','hahha')
                )
        );

        $this->db->query($this->db->insert('tableName')
                ->field('title','content')
                ->row(
                    array(
                        array('haha','hahha'),
                        array('haha')//?????????????????????
                    )
                )
        )
        );
         *
         *
         */


        /**
         * Delete
         * return change line num
         */
        /*
                $this->db->query($this->db
                        ->delete('tableName')
                        ->where('time < ?', time())
                );
                //delete id = 1 row
                $this->db->query($this->db
                    ->delete('tableName')
                    ->pri(1)
                );
                //delete id = 1 or id = 2 or id = 3 or id = 4 rows
                $this->db->query($this->db
                    ->delete('tableName')
                    ->pri(array(1,2,3,4))
                );

                //return change line num
                $this->db->query($this->db
                        ->update('tableName')
                        ->row(array('title'=>'11111'))
                        ->pre(1)
                );

        */
    }
}